package com.result;


import com.errorcode.BaseErrorCode;
import com.exception.AbstractException;

import java.util.Optional;

public class Results {
    /**
     * 构造成功响应
     */
    public static Result<Void> success() {
        return new Result<Void>()
                .setCode(Result.SUCCESS_CODE);
    }

    /**
     * 构造带返回数据的成功响应
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>()
                .setCode(Result.SUCCESS_CODE)
                .setData(data);
    }

    /**
     * 构建服务端失败响应
     */
    public static Result<Void> failure() {
        return new Result<Void>()
                .setCode(BaseErrorCode.SERVICE_ERROR.code())
                .setMessage(BaseErrorCode.SERVICE_ERROR.message());
    }

    /**
     * 通过 {@link AbstractException} 构建失败响应
     */
    public static Result<Void> failure(AbstractException abstractException) {
        String errorCode = Optional.ofNullable(abstractException.getErrorCode())
                .orElse(BaseErrorCode.SERVICE_ERROR.code());
        String errorMessage = Optional.ofNullable(abstractException.getErrorMessage())
                .orElse(BaseErrorCode.SERVICE_ERROR.message());
        return new Result<Void>()
                .setCode(errorCode)
                .setMessage(errorMessage);
    }

    /**
     * 通过 errorCode、errorMessage 构建失败响应
     */
    public static Result<Void> failure(String errorCode, String errorMessage) {
        return new Result<Void>()
                .setCode(errorCode)
                .setMessage(errorMessage);
    }



    /**
     * 通过 errorCode、errorMessage data构建失败响应
     */
    public static Result<String> failure(String errorCode, String errorMessage,String data) {
        return new Result<String>()
                .setCode(errorCode)
                .setMessage(errorMessage)
                .setData(data);
    }

    /**
     * 通过 errorMessage 构建未登录异常
     */
    public static Result<String> error(String msg){
        return new Result<String>()
                .setCode("A1003")
                .setMessage(msg)
                .setData("null");
    }

    /**
     *图片上传失败异常
     */
    public static Result<String> failure2(String errorCode, String errorMessage) {
        return new Result<String>()
                .setCode(errorCode)
                .setMessage(errorMessage);
    }

}
